package com.mall4j.cloud.multishop.controller.admin;

import cn.hutool.core.img.ImgUtil;
import cn.hutool.core.io.FileUtil;
import com.mall4j.cloud.common.database.dto.PageDTO;
import com.mall4j.cloud.common.database.vo.PageVO;
import com.mall4j.cloud.common.response.ServerResponseEntity;
import com.mall4j.cloud.common.security.AuthUserContext;
import com.mall4j.cloud.multishop.dto.HotSearchDTO;
import com.mall4j.cloud.multishop.model.HotSearch;
import com.mall4j.cloud.multishop.service.HotSearchService;
import com.mall4j.cloud.multishop.vo.HotSearchVO;
import ma.glasnost.orika.MapperFacade;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@RestController("adminTestController")
@RequestMapping("admin/test")
public class TestController {

    @Autowired
    private HotSearchService hotSearchService;

    @Autowired
    private MapperFacade mapperFacade;

    @PostMapping
    public ServerResponseEntity save(@Valid @RequestBody HotSearchDTO hotSearchDTO) {
        HotSearch hotSearch = mapperFacade.map(hotSearchDTO, HotSearch.class);
        hotSearch.setShopId(AuthUserContext.get().getTenantId());
        hotSearchService.save(hotSearch);
        return ServerResponseEntity.success();
    }

    @DeleteMapping
    public ServerResponseEntity delete(@RequestParam Long hotSearchId) {
        Long shopId = AuthUserContext.get().getTenantId();
        hotSearchService.deleteById(hotSearchId, shopId);
        return ServerResponseEntity.success();
    }

    @PutMapping
    public ServerResponseEntity update(@Valid @RequestBody HotSearchDTO hotSearchDTO) {
        HotSearch hotSearch = mapperFacade.map(hotSearchDTO, HotSearch.class);
        hotSearch.setShopId(AuthUserContext.get().getTenantId());
        hotSearchService.update(hotSearch);
        return ServerResponseEntity.success();
    }

    @GetMapping("page")
    public ServerResponseEntity page(@Valid PageDTO pageDTO, HotSearchDTO hotSearchDTO) {
        hotSearchDTO.setShopId(AuthUserContext.get().getTenantId());
        PageVO<HotSearchVO> hotSearchPage = hotSearchService.page(pageDTO, hotSearchDTO);
        return ServerResponseEntity.success(hotSearchPage);
    }

    //mybatisplus test
    @GetMapping
    public ServerResponseEntity getById(@RequestParam Long hotSearchId) {
        return ServerResponseEntity.success(hotSearchService.getById(hotSearchId));
    }


}
